<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=trim($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(isset($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','name','mobile','title','status','image','pcd','createtime');
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_cxjxiche_store',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['pcd']=json_decode($v['pcd'],true);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'门店管理'),
			array('op'=>'edit','title'=>'添加门店'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：超出坐标范围，不能接单','list'=>array(
			array('name'=>'id','title'=>'id','type'=>'string'),
			array('name'=>'title','title'=>'门店名称','type'=>'string'),
			array('name'=>'image','title'=>'Logo','type'=>'image'),
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'pcd.city','title'=>'城市','type'=>'string'),
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'status'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'门店名称','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'手机号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	$return['status']=array('1'=>'已审核','0'=>'待审核','2'=>'已驳回');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_cxjxiche_store',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['images']=json_decode($item['images'],true);
	$item['pcd']=json_decode($item['pcd'],true);
	$item['latlng']=json_decode($item['latlng'],true);
    $return['item']=$item;
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已驳回');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'门店设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'门店管理'),
			array('op'=>'edit','title'=>'添加门店')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'门店名称','type'=>'string'),
			array('name'=>'image','title'=>'Logo','type'=>'image'),
			array('name'=>'images','title'=>'门店幻灯片','type'=>'images'),
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'pcd','title'=>'门店城市','type'=>'pcd'),
			array('name'=>'address','title'=>'门店地址','type'=>'string'),
			array('name'=>'latlng','title'=>'坐标','type'=>'lat_lng'),
			array('name'=>'desc','title'=>'简介','type'=>'text'),
			array('name'=>'content','title'=>'门店详情','type'=>'richtext'),
			array('name'=>'mid','title'=>'绑定用户','type'=>'choose_title','action'=>'core/member.get_member/'),	
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'status'),			
		)
	);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$postdata['latlng']=ijson_encode($postdata['latlng']);
	$data=array_fields(array('name','mobile','title','latlng','image','mobile','images','pcd','mid','status','address','desc','content'),$postdata);	
	if(!$data['title']){
		exi('请填写门店名称','error');
	}
	if(!$data['name']){
		exi('请填写联系人','error');
	}
	if(!intval($data['mobile']) || strlen($data['mobile'])!=11){
		exi('请填写正确手机号','error');
	}
	$id=intval($postdata['id']);
	$data['latlng']=ijson_encode($data['latlng']);
	$data['images']=ijson_encode($data['images']);
	$data['pcd']=ijson_encode($data['pcd']);
	if($id){
		pdo_update('jy_cxjxiche_store',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_cxjxiche_store',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['stop'])){
		$update['stop']=intval($post['stop']);
	}
	if($update){
		pdo_update('jy_cxjxiche_store',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_cxjxiche_store',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_cxjxiche_store',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>